package com.example.iceman.stepcounterapp;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.iceman.stepcounterapp.db.StepDataBase;
import com.example.iceman.stepcounterapp.model.StepRecordEntity;
import com.example.iceman.stepcounterapp.widget.StepRecordView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 今日步行记录页面
 * Created by iceman on 16/3/11 17:17
 * 邮箱：xubin865@pingan.com.cn
 */
public class CurrentStepDetailActivity extends FragmentActivity {
    /**
     * 自定义控件,显示步行记录柱状图
     */
    @Bind(R.id.step_record_view)
    StepRecordView stepRecordView;
    /**
     * 显示每条步行记录
     */
    @Bind(R.id.record_frame)
    LinearLayout recordFrame;
    @Bind(R.id.scrollview)
    ScrollView scrollview;
    @Bind(R.id.left_btn)
    TextView leftBtn;
    /**
     * 当前选中的步行记录
     */
    private View currentItemView;
    /**
     * 步行记录数据
     */
    private StepRecordEntity[] recordEntities = new StepRecordEntity[24];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_step_detail_layout);
        ButterKnife.bind(this);
        initData();
//        testData();
        initView();

    }

    private void initData() {
        for (int i = 0; i < 24; i++) {
            StepRecordEntity entity = new StepRecordEntity();
            entity.stepInt = 0;
            entity.time = i + ":00";
            entity.heat = "0";
            recordEntities[i] = entity;
        }
        Calendar calendar = Calendar.getInstance();
        ArrayList<StepDataBase> todaySteps = StepSaveManager.getInstance().getStepDataByDay(calendar.get(Calendar.YEAR), calendar.get(Calendar.DAY_OF_YEAR));
        for (StepDataBase dateBaseEntity : todaySteps) {
            StepRecordEntity entity = new StepRecordEntity();
            entity.stepInt = dateBaseEntity.count;
            entity.time = dateBaseEntity.hour + ":00";
            entity.heat = String.valueOf(StepUtil.calculateHeat(entity.stepInt));
            try {
                int index = Integer.parseInt(dateBaseEntity.hour);
                recordEntities[index] = entity;
            } catch (Exception e) {

            }
        }
    }

    /**
     * 插入测试数据
     */
    private void testData() {
        Random ran = new Random(100);
        recordEntities = new StepRecordEntity[24];
        for (int i = 0; i < 24; i++) {
            StepRecordEntity entity = new StepRecordEntity();
            entity.stepInt = i < 5 ? 0 : ran.nextInt(10000);
            entity.time = i + ":00";
            entity.heat = String.valueOf(ran.nextInt(1000));
            recordEntities[i] = entity;
        }
    }

    private void initView() {
        stepRecordView.setRectNum(24);
        stepRecordView.setData(recordEntities);
        leftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addRecordViews();
        stepRecordView.setRecordSelectListener(new StepRecordView.RecordSelectListener() {
            @Override
            public void OnRecordSelect(StepRecordEntity entity) {
                focusCurrentRecordView(entity);
            }
        });

    }

    /**
     * 根据步行数据定位到应该显示的列表条目
     *
     * @param entity
     */
    private void focusCurrentRecordView(StepRecordEntity entity) {
        int size = recordFrame.getChildCount();
        for (int i = 0; i < size; i++) {
            View v = recordFrame.getChildAt(i);

            if (entity == v.getTag()) {
                if (v == currentItemView) {
                    return;
                }
                if (v != currentItemView && currentItemView != null) {
                    currentItemView.setSelected(false);
                }
                currentItemView = v;
                scrollview.smoothScrollTo(0, (int) v.getY() - DeviceUtil.dip2px(10));
                v.setSelected(true);
                return;
            }
        }

    }

    /**
     * 添加列表视图
     */
    private void addRecordViews() {
        LayoutInflater inflater = getLayoutInflater();
        recordFrame.removeAllViews();
        for (StepRecordEntity entity : recordEntities) {
            if (entity.stepInt == 0) {
                continue;
            }
            View view = inflater.inflate(R.layout.step_record_item, recordFrame, false);
            TextView time = (TextView) view.findViewById(R.id.time);
            TextView step = (TextView) view.findViewById(R.id.step);
            TextView heat = (TextView) view.findViewById(R.id.heat);
            time.setText(entity.time);
            step.setText(String.valueOf(entity.stepInt));
            heat.setText(entity.heat);
            recordFrame.addView(view);
            view.setTag(entity);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (v == currentItemView) {
                        return;
                    } else {
                        if (currentItemView != null) {
                            currentItemView.setSelected(false);
                        }
                        currentItemView = v;
                        currentItemView.setSelected(true);
                    }
                    StepRecordEntity entity = (StepRecordEntity) v.getTag();
                    stepRecordView.setCurrentRectIndex(findIndexInArray(entity));
                }
            });
        }
    }

    /**
     * 根据步行数据找到其在列表中的index
     *
     * @param entity
     * @return
     */
    private int findIndexInArray(StepRecordEntity entity) {
        for (int i = 0; i < recordEntities.length; i++) {
            if (entity == recordEntities[i]) {
                return i;
            }
        }
        return 0;
    }
}
